package com.example.demo.controller;

import com.example.demo.model.Student;

import com.example.demo.model.Teacher;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;


import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("student")
public class StudentController {

        private static List<Student> studentList = new ArrayList<>();
        static {
            Student s1 = new Student();
            s1.setId(1);
            s1.setName("Tom");
            studentList.add(s1);
            Student s2 = new Student();
            s2.setId(2);
            s2.setName("Jerry");
            studentList.add(s2);
        }
        @RequestMapping("list")
        public String list(ModelMap modelMap) {
            modelMap.put("studentList", studentList);
            return "student/list";
        }
        @RequestMapping("delete/{id}")
        public String delete(@PathVariable Integer id) {
            studentList.remove(id - 1);
            return "redirect:/student/list";
        }
        @RequestMapping("save")
        public String save(Student s) {
            s.setId(studentList.size() + 1);
            studentList.add(s);
            return "redirect:/student/list";
        }
        @RequestMapping("edit/{id}")
        public String edit(@PathVariable Integer id, ModelMap map) {
            Student s = studentList.get(id - 1);
            map.put("student", s);
            return "edit";
        }
        @RequestMapping("update")
        public String update(Student s) {
            for (Student student: studentList) {
                if (student.getId().equals(s.getId())) {
                    student.setName(s.getName());
                }
            }
            return "redirect:/student/list";
        }
    }

